/**
 * 奖品表 编辑页
 *
 * 作者 admin
 * 日期 2024-08-06 09:41:04
 */
layui.use(['jquery', 'febs', 'form', 'validate', 'fileUpload','commonJS'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        commonJS=layui.commonJS,
        $view = $('#baseGoods-edit'),
        validate = layui.validate,
        fileUpload = layui.fileUpload;
    var imgData=[]

    form.verify(validate);
    form.render();
    initFormValue();
    initGoodsUnit();
    var fileUploadDiv = fileUpload.render({
        elem: '#fileUpload',
        url: ctx + 'base/upload',
        type: 0,//0-图片，1-视频，2-其他
        defaultData:imgData,
        maxNum:1,
        done: function (res) {
            // console.log('上传图片返回' + JSON.stringify(res));
            $view.find('input[name="fileName"]').val(res.fileName);
            $view.find('input[name="filePath"]').val(res.filePath);
            $view.find('input[name="smallPath"]').val(res.smallPath);
            form.render();
        },
    });


    function initGoodsUnit() {
        febs.initDictItemSelect($('#goodsUnit'),'SAFETY_INCENTIVE_GOODS_UNIT',null);
        form.render();
    }


    function initFormValue() {
        if (baseGoodsData) {
            form.val("baseGoods-edit-form", {
                "goodsId": baseGoodsData.goodsId,
                "tenantId": baseGoodsData.tenantId,
                "goodsName": baseGoodsData.goodsName,
                "goodsPrice": baseGoodsData.goodsPrice,
                "goodsUnit": baseGoodsData.goodsUnit,
                "exchangeScore": baseGoodsData.exchangeScore,
                "inventory": baseGoodsData.inventory,
                "lockNumber": baseGoodsData.lockNumber,
                "issuedNumber": baseGoodsData.issuedNumber,
                // "status": baseGoodsData.status,
                "fileName": baseGoodsData.fileName,
                "filePath": baseGoodsData.filePath,
                "smallPath": baseGoodsData.smallPath,

            });
            if(baseGoodsData.status === '1'){
                $view.find('input[type=checkbox]').attr("checked","true");
                form.render('checkbox');
            }
            if(!commonJS.isEmpty(baseGoodsData.filePath)){
                imgData.push({
                    filePath: baseGoodsData.filePath,
                    fileName:baseGoodsData.fileName
                })
            }
        } else {
            form.val("baseGoods-edit-form", {
                "tenantId": POPUP_DATA.tenantId,
                "status": true
            });
        }
    }

    form.on('submit(baseGoods-edit-form-submit)', function (data) {
        // console.log("更改啦" + JSON.stringify(data.field))
        if (data.field.status == "on") {
            data.field.status = '1';
        } else {
            data.field.status = '0';
        }
        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        febs.modal.confirm('修改奖品', '确定修改奖品？', function () {
            if (data.goodsId && data.goodsId.length > 0) {
                febs.postArray(ctx + 'baseGoods/update', data, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                    $('#febs-baseGoods').find('#query').trigger('click');
                    // febs.reloadPage();
                });
            } else {
                febs.postArray(ctx + 'baseGoods', data, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    $('#febs-baseGoods').find('#query').trigger('click');
                });
            }
        });

        $('#febs-baseGoods').find('#query').trigger('click');
    }



})
;